<div class="fsh-rightPanel">
    <label> <input type="password" style="display: none"></label>
    <input type="hidden" id="class_id" value="<%=squad_id%>"/>
    <div class="layui-anim layui-anim-upbit">
        <div class="layui-form-item wx-search" id="list_form">
            <div class="layui-inline">
                <input type="text" name="nickname" placeholder="请输入名称" autocomplete="off" class="layui-input">
            </div>
            <button class="layui-btn btn-primary" id="search">搜索</button>
        </div>
        <table id="schoolList" class="layui-hide" lay-filter="schoolList"></table>
    </div>
</div>
<script type="text/html" id="toolBar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add_school">+添加学校</button>
    </div>
</script>
<script>
    layui.use(['layer', 'table'], function () {
        var layer = layui.layer;
        var table = layui.table;
        var tableObj2 = table.render({
            elem: '#schoolList',
            //开启头部工具栏，并为其绑定左侧模板，
            toolbar: '#toolBar',
            defaultToolbar: [],
            request: {
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            align: 'center'
            , cols: [[ //表头
                {
                    field: 'logo', title: 'LOGO', align: 'center', templet: function (d) {
                        return d.logo !== undefined ? '<div><img src="' + d.logo + '" style="height:100px;width:100px;" alt=""/></div>' : '';
                    }
                },
                {field: 'code', title: '学校编号', align: 'center'},
                {field: 'schoolName', title: '学校名称', align: 'center'},
                {field: 'location', title: '所属城市', align: 'center'},
                {field: 'department', title: '所属部门', align: 'center'},
                {field: 'level', title: '类别', align: 'center'},
                {field: 'createDate', title: '分配日期', align: 'center'}
                , {
                    field: 'lock', title: '操作', width: 100, align: 'center', unresize: true,
                    templet: function (d) {
                            return '<a href="javascript:;" class="font-primary" lay-event="del">删除</a>'
                    }
                }
            ]], data: [{
                "code": "10001"
                , "schoolName": "北京大学"
                , "location": "北京"
                , "department": "教育部"
                , "level": "本科"
                , "createDate": "2016-10-14"
            }, {
                "code": "10001"
                , "schoolName": "清华大学"
                , "location": "北京"
                , "department": "教育部"
                , "level": "本科"
                , "createDate": "2016-10-14"
            }]
            , page: true

        });

        // 检索
        $("#search").click(function () {
            reloadTable(tableObj2);
        });

        //头工具栏事件
        table.on('toolbar(schoolList)', function (obj) {
            var layEvent = obj.event; //获得 lay-event 对应的值
            // 添加
            if (layEvent === 'add_school') {
                showDialog({
                    title: '添加学校'
                    , template: 'views/school/class/template/school_multi.html'
                    , width: 800
                    , height: 600
                    , yes: function (index, layero) {
                       // var checkData = table.checkStatus('table2').data;
                        layer.close(index);
                        // var data = {};
                        // for (var i = 0; i < checkData.length; i++) {
                        //     data.user_id = checkData[i].user_id;
                        //     data.squad_id = classId;
                        //     $.ajax({
                        //         url: context + '/api/student/updateSquad',
                        //         type: 'PATCH',
                        //         async: false,
                        //         dataType: 'json',
                        //         contentType: "application/json;charset-UTF-8",
                        //         data: JSON.stringify(data),
                        //         success: function (result) {
                        //             if (result.success) {
                        //                 layer.msg("添加成功");
                        //                 reloadTable(tableObj2);
                        //             } else {
                        //                 layer.msg(result.msg);
                        //             }
                        //         },
                        //         error: function () {
                        //             layer.msg("添加异常");
                        //         }
                        //     })
                        // }
                    }

                })
            }
        });
        //表格内部操作按钮监听
        table.on('tool(studentList)', function (obj) { //注：tool是工具条事件名，mainList是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            // 删除
            if (layEvent === 'del') {
                layer.confirm('确认移除此学生？', {
                    icon: 7,
                    title: "提示",
                    btn: ['确认', '取消'] //按钮
                }, function () {
                    var json = {
                        user_id: data.user_id,
                        squad_id: 0
                    };
                    $.ajax({
                        url: context + '/api/student/updateSquad',
                        type: 'PATCH',
                        async: false,
                        dataType: 'json',
                        contentType: "application/json;charset-UTF-8",
                        data: JSON.stringify(json),
                        success: function (result) {
                            if (result.success) {
                                layer.msg("移除成功");
                                reloadTable(tableObj2);
                            } else {
                                layer.msg(result.msg);
                            }
                        },
                        error: function () {
                            layer.msg("移除异常");
                        }
                    })
                }, function () {
                    layer.closeAll();
                });
            }
        });

    });
</script>
